/*
 * Original file:
 * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com/
 * All rights reserved.
 *
 * Port to Contiki:
 * Copyright (c) 2013, ADVANSEE - http://www.advansee.com/
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */
/**
 * \addtogroup cc2538-examples
 * @{
 *
 * \defgroup cc2538-ccm-test cc2538 AES-CCM Test Project
 *
 *   AES-CCM access example for CC2538-based platforms
 *
 *   This example shows how AES-CCM should be used. The example also verifies
 *   the AES-CCM functionality.
 *
 * @{
 *
 * \file
 *     Example demonstrating AES-CCM
 */
#include "contiki.h"
#include "sys/rtimer.h"
#include "dev/rom-util.h"
#include "dev/ccm.h"

#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
/*---------------------------------------------------------------------------*/
#define NONCE_MAX_LEN   13
#define ADATA_MAX_LEN   32
#define MDATA_MAX_LEN   40
#define MIC_MAX_LEN     16
/*---------------------------------------------------------------------------*/
PROCESS(ccm_test_process, "ccm test process");
AUTOSTART_PROCESSES(&ccm_test_process);
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(ccm_test_process, ev, data)
{
  static const char *const str_res[] = {
    "success",
    "invalid param",
    "NULL error",
    "resource in use",
    "DMA bus error",
    "keystore read error",
    "keystore write error",
    "authentication failed"
  };
  static const uint8_t keys128[][128 / 8] = {
    { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
    { 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
      0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf }
  };
  static const uint8_t keys192[][192 / 8] = {
    { 0x26, 0x51, 0x1f, 0xb5, 0x1f, 0xcf, 0xa7, 0x5c,
      0xb4, 0xb4, 0x4d, 0xa7, 0x5a, 0x6e, 0x5a, 0x0e,
      0xb8, 0xd9, 0xc8, 0xf3, 0xb9, 0x06, 0xf8, 0x86 },
    { 0x08, 0x6e, 0x29, 0x67, 0xcd, 0xe9, 0x9e, 0x90,
      0xfa, 0xae, 0xa8, 0xa9, 0x4e, 0x16, 0x8b, 0xf0,
      0xe0, 0x66, 0xc5, 0x03, 0xa8, 0x49, 0xa9, 0xf3 },
    { 0xce, 0xb0, 0x09, 0xae, 0xa4, 0x45, 0x44, 0x51,
      0xfe, 0xad, 0xf0, 0xe6, 0xb3, 0x6f, 0x45, 0x55,
      0x5d, 0xd0, 0x47, 0x23, 0xba, 0xa4, 0x48, 0xe8 },
    { 0x11, 0xfd, 0x45, 0x74, 0x3d, 0x94, 0x6e, 0x6d,
      0x37, 0x34, 0x1f, 0xec, 0x49, 0x94, 0x7e, 0x8c,
      0x70, 0x48, 0x24, 0x94, 0xa8, 0xf0, 0x7f, 0xcc }
  };
  static const uint8_t keys256[][256 / 8] = {
    { 0x26, 0x51, 0x1f, 0xb5, 0x1f, 0xcf, 0xa7, 0x5c,
      0xb4, 0xb4, 0x4d, 0xa7, 0x5a, 0x6e, 0x5a, 0x0e,
      0xb8, 0xd9, 0xc8, 0xf3, 0xb9, 0x06, 0xf8, 0x86,
      0xdf, 0x3b, 0xa3, 0xe6, 0xda, 0x3a, 0x13, 0x89 },
    { 0xc6, 0xc1, 0x4c, 0x65, 0x5e, 0x52, 0xc8, 0xa4,
      0xc7, 0xe8, 0xd5, 0x4e, 0x97, 0x4d, 0x69, 0x8e,
      0x1f, 0x21, 0xee, 0x3b, 0xa7, 0x17, 0xa0, 0xad,
      0xfa, 0x61, 0x36, 0xd0, 0x26, 0x68, 0xc4, 0x76 },
    { 0x55, 0x35, 0x21, 0xa7, 0x65, 0xab, 0x0c, 0x3f,
      0xd2, 0x03, 0x65, 0x4e, 0x99, 0x16, 0x33, 0x0e,
      0x18, 0x9b, 0xdf, 0x95, 0x1f, 0xee, 0xe9, 0xb4,
      0x4b, 0x10, 0xda, 0x20, 0x8f, 0xee, 0x7a, 0xcf },
    { 0x90, 0x74, 0xb1, 0xae, 0x4c, 0xa3, 0x34, 0x2f,
      0xe5, 0xbf, 0x6f, 0x14, 0xbc, 0xf2, 0xf2, 0x79,
      0x04, 0xf0, 0xb1, 0x51, 0x79, 0xd9, 0x5a, 0x65,
      0x4f, 0x61, 0xe6, 0x99, 0x69, 0x2e, 0x6f, 0x71 }
  };
  static const struct {
    const void *keys;
    uint8_t key_size;
    uint8_t count;
  } keys[] = {
    { keys128, AES_KEY_STORE_SIZE_KEY_SIZE_128,
      sizeof(keys128) / sizeof(keys128[0]) },
    { keys192, AES_KEY_STORE_SIZE_KEY_SIZE_192,
      sizeof(keys192) / sizeof(keys192[0]) },
    { keys256, AES_KEY_STORE_SIZE_KEY_SIZE_256,
      sizeof(keys256) / sizeof(keys256[0]) }
  };
  static const struct {
    bool encrypt;
    uint8_t len_len;
    uint8_t key_size_index;
    uint8_t key_area;
    uint8_t nonce[NONCE_MAX_LEN];
    uint8_t adata[ADATA_MAX_LEN];
    uint16_t adata_len;
    uint8_t mdata[MDATA_MAX_LEN];
    uint16_t mdata_len;
    uint8_t mic_len;
    uint8_t expected[MDATA_MAX_LEN];
  } vectors[] = {
    {
      true, /* encrypt */
      2, /* len_len */
      0, /* key_size_index */
      0, /* key_area */
      { 0x00, 0x00, 0xf0, 0xe0, 0xd0, 0xc0, 0xb0, 0xa0,
        0x00, 0x00, 0x00, 0x00, 0x05 }, /* nonce */
      {}, /* adata */
      0, /* adata_len */
      { 0x14, 0xaa, 0xbb, 0x00, 0x00, 0x01, 0x02, 0x03,
        0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
        0x0c, 0x0d, 0x0e, 0x0f }, /* mdata */
      20, /* mdata_len */
      0, /* mic_len */
      { 0x92, 0xe8, 0xad, 0xca, 0x53, 0x81, 0xbf, 0xd0,
        0x5b, 0xdd, 0xf3, 0x61, 0x09, 0x09, 0x82, 0xe6,
        0x2c, 0x61, 0x01, 0x4e } /* expected */
    }, {
      true, /* encrypt */
      2, /* len_len */
      0, /* key_size_index */
      1, /* key_area */
      { 0xac, 0xde, 0x48, 0x00, 0x00, 0x00, 0x00, 0x01,
        0x00, 0x00, 0x00, 0x05, 0x02 }, /* nonce */
      { 0x08, 0xd0, 0x84, 0x21, 0x43, 0x01, 0x00, 0x00,
        0x00, 0x00, 0x48, 0xde, 0xac, 0x02, 0x05, 0x00,
        0x00, 0x00, 0x55, 0xcf, 0x00, 0x00, 0x51, 0x52,
        0x53, 0x54 }, /* adata */
      26, /* adata_len */
      {}, /* mdata */
      0, /* mdata_len */
      8, /* mic_len */
      { 0x22, 0x3b, 0xc1, 0xec, 0x84, 0x1a, 0xb5, 0x53 } /* expected */
    }, {
      true, /* encrypt */
      2, /* len_len */
      0, /* key_size_index */
      0, /* key_area */
      { 0x00, 0x00, 0xf0, 0xe0, 0xd0, 0xc0, 0xb0, 0xa0,
        0x00, 0x00, 0x00, 0x00, 0x05 }, /* nonce */
      { 0x69, 0x98, 0x03, 0x33, 0x63, 0xbb, 0xaa, 0x01,
        0x00, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x03 }, /* adata */
      15, /* adata_len */
      { 0x14, 0xaa, 0xbb, 0x00, 0x00, 0x01, 0x02, 0x03,
        0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
        0x0c, 0x0d, 0x0e, 0x0f }, /* mdata */
      20, /* mdata_len */
      4, /* mic_len */
      { 0x92, 0xe8, 0xad, 0xca, 0x53, 0x81, 0xbf, 0xd0,
        0x5b, 0xdd, 0xf3, 0x61, 0x09, 0x09, 0x82, 0xe6,
        0x2c, 0x61, 0x01, 0x4e, 0x7b, 0x34, 0x4f, 0x09 } /* expected */
    }, {
      false, /* decrypt */
      2, /* len_len */
      0, /* key_size_index */
      0, /* key_area */
      { 0x00, 0x00, 0xf0, 0xe0, 0xd0, 0xc0, 0xb0, 0xa0,
        0x00, 0x00, 0x00, 0x00, 0x05 }, /* nonce */
      {}, /* adata */
      0, /* adata_len */
      { 0x92, 0xe8, 0xad, 0xca, 0x53, 0x81, 0xbf, 0xd0,
        0x5b, 0xdd, 0xf3, 0x61, 0x09, 0x09, 0x82, 0xe6,
        0x2c, 0x61, 0x01, 0x4e }, /* mdata */
      20, /* mdata_len */
      0, /* mic_len */
      { 0x14, 0xaa, 0xbb, 0x00, 0x00, 0x01, 0x02, 0x03,
        0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
        0x0c, 0x0d, 0x0e, 0x0f } /* expected */
    }, {
      false, /* decrypt */
      2, /* len_len */
      0, /* key_size_index */
      1, /* key_area */
      { 0xac, 0xde, 0x48, 0x00, 0x00, 0x00, 0x00, 0x01,
        0x00, 0x00, 0x00, 0x05, 0x02 }, /* nonce */
      { 0x08, 0xd0, 0x84, 0x21, 0x43, 0x01, 0x00, 0x00,
        0x00, 0x00, 0x48, 0xde, 0xac, 0x02, 0x05, 0x00,
        0x00, 0x00, 0x55, 0xcf, 0x00, 0x00, 0x51, 0x52,
        0x53, 0x54 }, /* adata */
      26, /* adata_len */
      { 0x22, 0x3b, 0xc1, 0xec, 0x84, 0x1a, 0xb5, 0x53 }, /* mdata */
      8, /* mdata_len */
      8, /* mic_len */
      {} /* expected */
    }, {
      false, /* decrypt */
      2, /* len_len */
      0, /* key_size_index */
      0, /* key_area */
      { 0x00, 0x00, 0xf0, 0xe0, 0xd0, 0xc0, 0xb0, 0xa0,
        0x00, 0x00, 0x00, 0x00, 0x05 }, /* nonce */
      { 0x69, 0x98, 0x03, 0x33, 0x63, 0xbb, 0xaa, 0x01,
        0x00, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x03 }, /* adata */
      15, /* adata_len */
      { 0x92, 0xe8, 0xad, 0xca, 0x53, 0x81, 0xbf, 0xd0,
        0x5b, 0xdd, 0xf3, 0x61, 0x09, 0x09, 0x82, 0xe6,
        0x2c, 0x61, 0x01, 0x4e, 0x7b, 0x34, 0x4f, 0x09 }, /* mdata */
      24, /* mdata_len */
      4, /* mic_len */
      { 0x14, 0xaa, 0xbb, 0x00, 0x00, 0x01, 0x02, 0x03,
        0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
        0x0c, 0x0d, 0x0e, 0x0f } /* expected */
    }, {
      true, /* encrypt */
      2, /* len_len */
      1, /* key_size_index */
      0, /* key_area */
      { 0x15, 0xb3, 0x69, 0x88, 0x96, 0x99, 0xb6, 0xde,
        0x1f, 0xa3, 0xee, 0x73, 0xe5 }, /* nonce */
      {}, /* adata */
      0, /* adata_len */
      { 0x39, 0xf0, 0x8a, 0x2a, 0xf1, 0xd8, 0xda, 0x62,
        0x12, 0x55, 0x06, 0x39, 0xb9, 0x1f, 0xb2, 0x57,
        0x3e, 0x39, 0xa8, 0xeb, 0x5d, 0x80, 0x1d, 0xe8 }, /* mdata */
      24, /* mdata_len */
      16, /* mic_len */
      { 0x63, 0x42, 0xb8, 0x70, 0x0e, 0xde, 0xc9, 0x7a,
        0x96, 0x0e, 0xb1, 0x6e, 0x7c, 0xb1, 0xeb, 0x44,
        0x12, 0xfb, 0x4e, 0x26, 0x3d, 0xdd, 0x22, 0x06,
        0xb0, 0x90, 0x15, 0x5d, 0x34, 0xa7, 0x6c, 0x83,
        0x24, 0xe5, 0x55, 0x0c, 0x3e, 0xf4, 0x26, 0xed } /* expected */
    }, {
      true, /* encrypt */
      2, /* len_len */
      1, /* key_size_index */
      2, /* key_area */
      { 0x92, 0x95, 0x42, 0xcd, 0x69, 0x0f, 0x1b, 0xab,
        0xcf, 0x16, 0x96, 0xcb, 0x03 }, /* nonce */
      { 0x58, 0xf7, 0x0b, 0xab, 0x24, 0xe0, 0xa6, 0x13,
        0x7e, 0x5c, 0xd3, 0xeb, 0x18, 0x65, 0x6f, 0x2b,
        0x5c, 0xcd, 0xdc, 0x3f, 0x53, 0x8a, 0x00, 0x00,
        0xc6, 0x51, 0x90, 0xe4, 0xa3, 0x66, 0x8e, 0x71 }, /* adata */
      32, /* adata_len */
      {}, /* mdata */
      0, /* mdata_len */
      16, /* mic_len */
      { 0x3b, 0xf9, 0xd9, 0x3a, 0xf6, 0xff, 0xac, 0x9a,
        0xc8, 0x4c, 0xd3, 0x20, 0x2d, 0x4e, 0x0c, 0xc8 } /* expected */
    }, {
      true, /* encrypt */
      8, /* len_len */
      1, /* key_size_index */
      4, /* key_area */
      { 0x76, 0x40, 0x43, 0xc4, 0x94, 0x60, 0xb7 }, /* nonce */
      { 0x6e, 0x80, 0xdd, 0x7f, 0x1b, 0xad, 0xf3, 0xa1,
        0xc9, 0xab, 0x25, 0xc7, 0x5f, 0x10, 0xbd, 0xe7,
        0x8c, 0x23, 0xfa, 0x0e, 0xb8, 0xf9, 0xaa, 0xa5,
        0x3a, 0xde, 0xfb, 0xf4, 0xcb, 0xf7, 0x8f, 0xe4 }, /* adata */
      32, /* adata_len */
      { 0xc8, 0xd2, 0x75, 0xf9, 0x19, 0xe1, 0x7d, 0x7f,
        0xe6, 0x9c, 0x2a, 0x1f, 0x58, 0x93, 0x9d, 0xfe,
        0x4d, 0x40, 0x37, 0x91, 0xb5, 0xdf, 0x13, 0x10 }, /* mdata */
      24, /* mdata_len */
      16, /* mic_len */
      { 0x8a, 0x0f, 0x3d, 0x82, 0x29, 0xe4, 0x8e, 0x74,
        0x87, 0xfd, 0x95, 0xa2, 0x8a, 0xd3, 0x92, 0xc8,
        0x0b, 0x36, 0x81, 0xd4, 0xfb, 0xc7, 0xbb, 0xfd,
        0x2d, 0xd6, 0xef, 0x1c, 0x45, 0xd4, 0xcc, 0xb7,
        0x23, 0xdc, 0x07, 0x44, 0x14, 0xdb, 0x50, 0x6d } /* expected */
    }, {
      true, /* encrypt */
      2, /* len_len */
      1, /* key_size_index */
      6, /* key_area */
      { 0xc6, 0xae, 0xeb, 0xcb, 0x14, 0x6c, 0xfa, 0xfa,
        0xae, 0x66, 0xf7, 0x8a, 0xab }, /* nonce */
      { 0x7d, 0xc8, 0xc5, 0x21, 0x44, 0xa7, 0xcb, 0x65,
        0xb3, 0xe5, 0xa8, 0x46, 0xe8, 0xfd, 0x7e, 0xae,
        0x37, 0xbf, 0x69, 0x96, 0xc2, 0x99, 0xb5, 0x6e,
        0x49, 0x14, 0x4e, 0xbf, 0x43, 0xa1, 0x77, 0x0f }, /* adata */
      32, /* adata_len */
      { 0xee, 0x7e, 0x60, 0x75, 0xba, 0x52, 0x84, 0x6d,
        0xe5, 0xd6, 0x25, 0x49, 0x59, 0xa1, 0x8a, 0xff,
        0xc4, 0xfa, 0xf5, 0x9c, 0x8e, 0xf6, 0x34, 0x89 }, /* mdata */
      24, /* mdata_len */
      4, /* mic_len */
      { 0x13, 0x7d, 0x9d, 0xa5, 0x9b, 0xaf, 0x5c, 0xbf,
        0xd4, 0x66, 0x20, 0xc5, 0xf2, 0x98, 0xfc, 0x76,
        0x6d, 0xe1, 0x0a, 0xc6, 0x8e, 0x77, 0x4e, 0xdf,
        0x1f, 0x2c, 0x5b, 0xad } /* expected */
    }, {
      false, /* decrypt */
      2, /* len_len */
      1, /* key_size_index */
      0, /* key_area */
      { 0x15, 0xb3, 0x69, 0x88, 0x96, 0x99, 0xb6, 0xde,
        0x1f, 0xa3, 0xee, 0x73, 0xe5 }, /* nonce */
      {}, /* adata */
      0, /* adata_len */
      { 0x63, 0x42, 0xb8, 0x70, 0x0e, 0xde, 0xc9, 0x7a,
        0x96, 0x0e, 0xb1, 0x6e, 0x7c, 0xb1, 0xeb, 0x44,
        0x12, 0xfb, 0x4e, 0x26, 0x3d, 0xdd, 0x22, 0x06,
        0xb0, 0x90, 0x15, 0x5d, 0x34, 0xa7, 0x6c, 0x83,
        0x24, 0xe5, 0x55, 0x0c, 0x3e, 0xf4, 0x26, 0xed }, /* mdata */
      40, /* mdata_len */
      16, /* mic_len */
      { 0x39, 0xf0, 0x8a, 0x2a, 0xf1, 0xd8, 0xda, 0x62,
        0x12, 0x55, 0x06, 0x39, 0xb9, 0x1f, 0xb2, 0x57,
        0x3e, 0x39, 0xa8, 0xeb, 0x5d, 0x80, 0x1d, 0xe8 } /* expected */
    }, {
      false, /* decrypt */
      2, /* len_len */
      1, /* key_size_index */
      2, /* key_area */
      { 0x92, 0x95, 0x42, 0xcd, 0x69, 0x0f, 0x1b, 0xab,
        0xcf, 0x16, 0x96, 0xcb, 0x03 }, /* nonce */
      { 0x58, 0xf7, 0x0b, 0xab, 0x24, 0xe0, 0xa6, 0x13,
        0x7e, 0x5c, 0xd3, 0xeb, 0x18, 0x65, 0x6f, 0x2b,
        0x5c, 0xcd, 0xdc, 0x3f, 0x53, 0x8a, 0x00, 0x00,
        0xc6, 0x51, 0x90, 0xe4, 0xa3, 0x66, 0x8e, 0x71 }, /* adata */
      32, /* adata_len */
      { 0x3b, 0xf9, 0xd9, 0x3a, 0xf6, 0xff, 0xac, 0x9a,
        0xc8, 0x4c, 0xd3, 0x20, 0x2d, 0x4e, 0x0c, 0xc8 }, /* mdata */
      16, /* mdata_len */
      16, /* mic_len */
      {} /* expected */
    }, {
      false, /* decrypt */
      8, /* len_len */
      1, /* key_size_index */
      4, /* key_area */
      { 0x76, 0x40, 0x43, 0xc4, 0x94, 0x60, 0xb7 }, /* nonce */
      { 0x6e, 0x80, 0xdd, 0x7f, 0x1b, 0xad, 0xf3, 0xa1,
        0xc9, 0xab, 0x25, 0xc7, 0x5f, 0x10, 0xbd, 0xe7,
        0x8c, 0x23, 0xfa, 0x0e, 0xb8, 0xf9, 0xaa, 0xa5,
        0x3a, 0xde, 0xfb, 0xf4, 0xcb, 0xf7, 0x8f, 0xe4 }, /* adata */
      32, /* adata_len */
      { 0x8a, 0x0f, 0x3d, 0x82, 0x29, 0xe4, 0x8e, 0x74,
        0x87, 0xfd, 0x95, 0xa2, 0x8a, 0xd3, 0x92, 0xc8,
        0x0b, 0x36, 0x81, 0xd4, 0xfb, 0xc7, 0xbb, 0xfd,
        0x2d, 0xd6, 0xef, 0x1c, 0x45, 0xd4, 0xcc, 0xb7,
        0x23, 0xdc, 0x07, 0x44, 0x14, 0xdb, 0x50, 0x6d }, /* mdata */
      40, /* mdata_len */
      16, /* mic_len */
      { 0xc8, 0xd2, 0x75, 0xf9, 0x19, 0xe1, 0x7d, 0x7f,
        0xe6, 0x9c, 0x2a, 0x1f, 0x58, 0x93, 0x9d, 0xfe,
        0x4d, 0x40, 0x37, 0x91, 0xb5, 0xdf, 0x13, 0x10 } /* expected */
    }, {
      false, /* decrypt */
      2, /* len_len */
      1, /* key_size_index */
      6, /* key_area */
      { 0xc6, 0xae, 0xeb, 0xcb, 0x14, 0x6c, 0xfa, 0xfa,
        0xae, 0x66, 0xf7, 0x8a, 0xab }, /* nonce */
      { 0x7d, 0xc8, 0xc5, 0x21, 0x44, 0xa7, 0xcb, 0x65,
        0xb3, 0xe5, 0xa8, 0x46, 0xe8, 0xfd, 0x7e, 0xae,
        0x37, 0xbf, 0x69, 0x96, 0xc2, 0x99, 0xb5, 0x6e,
        0x49, 0x14, 0x4e, 0xbf, 0x43, 0xa1, 0x77, 0x0f }, /* adata */
      32, /* adata_len */
      { 0x13, 0x7d, 0x9d, 0xa5, 0x9b, 0xaf, 0x5c, 0xbf,
        0xd4, 0x66, 0x20, 0xc5, 0xf2, 0x98, 0xfc, 0x76,
        0x6d, 0xe1, 0x0a, 0xc6, 0x8e, 0x77, 0x4e, 0xdf,
        0x1f, 0x2c, 0x5b, 0xad }, /* mdata */
      28, /* mdata_len */
      4, /* mic_len */
      { 0xee, 0x7e, 0x60, 0x75, 0xba, 0x52, 0x84, 0x6d,
        0xe5, 0xd6, 0x25, 0x49, 0x59, 0xa1, 0x8a, 0xff,
        0xc4, 0xfa, 0xf5, 0x9c, 0x8e, 0xf6, 0x34, 0x89 } /* expected */
    }, {
      true, /* encrypt */
      2, /* len_len */
      2, /* key_size_index */
      0, /* key_area */
      { 0x72, 0xa6, 0x0f, 0x34, 0x5a, 0x19, 0x78, 0xfb,
        0x40, 0xf2, 0x8a, 0x2f, 0xa4 }, /* nonce */
      {}, /* adata */
      0, /* adata_len */
      { 0x30, 0xd5, 0x6f, 0xf2, 0xa2, 0x5b, 0x83, 0xfe,
        0xe7, 0x91, 0x11, 0x0f, 0xca, 0xea, 0x48, 0xe4,
        0x1d, 0xb7, 0xc7, 0xf0, 0x98, 0xa8, 0x10, 0x00 }, /* mdata */
      24, /* mdata_len */
      16, /* mic_len */
      { 0x55, 0xf0, 0x68, 0xc0, 0xbb, 0xba, 0x8b, 0x59,
        0x80, 0x13, 0xdd, 0x18, 0x41, 0xfd, 0x74, 0x0f,
        0xda, 0x29, 0x02, 0x32, 0x21, 0x48, 0xab, 0x5e,
        0x93, 0x57, 0x53, 0xe6, 0x01, 0xb7, 0x9d, 0xb4,
        0xae, 0x73, 0x0b, 0x6a, 0xe3, 0x50, 0x07, 0x31 } /* expected */
    }, {
      true, /* encrypt */
      2, /* len_len */
      2, /* key_size_index */
      2, /* key_area */
      { 0x29, 0x1e, 0x91, 0xb1, 0x9d, 0xe5, 0x18, 0xcd,
        0x78, 0x06, 0xde, 0x44, 0xf6 }, /* nonce */
      { 0xb4, 0xf8, 0x32, 0x69, 0x44, 0xa4, 0x5d, 0x95,
        0xf9, 0x18, 0x87, 0xc2, 0xa6, 0xac, 0x36, 0xb6,
        0x0e, 0xea, 0x5e, 0xde, 0xf8, 0x4c, 0x1c, 0x35,
        0x81, 0x46, 0xa6, 0x66, 0xb6, 0x87, 0x83, 0x35 }, /* adata */
      32, /* adata_len */
      {}, /* mdata */
      0, /* mdata_len */
      16, /* mic_len */
      { 0xca, 0x48, 0x2c, 0x67, 0x4b, 0x59, 0x90, 0x46,
        0xcc, 0x7d, 0x7e, 0xe0, 0xd0, 0x0e, 0xec, 0x1e } /* expected */
    }, {
      true, /* encrypt */
      8, /* len_len */
      2, /* key_size_index */
      4, /* key_area */
      { 0xaa, 0xa2, 0x3f, 0x10, 0x16, 0x47, 0xd8 }, /* nonce */
      { 0xa3, 0x55, 0xd4, 0xc6, 0x11, 0x81, 0x2e, 0x5f,
        0x92, 0x58, 0xd7, 0x18, 0x8b, 0x3d, 0xf8, 0x85,
        0x14, 0x77, 0x09, 0x4f, 0xfc, 0x2a, 0xf2, 0xcf,
        0x0c, 0x86, 0x70, 0xdb, 0x90, 0x3f, 0xbb, 0xe0 }, /* adata */
      32, /* adata_len */
      { 0x64, 0x4e, 0xb3, 0x4b, 0x9a, 0x12, 0x6e, 0x43,
        0x7b, 0x5e, 0x01, 0x5e, 0xea, 0x14, 0x1c, 0xa1,
        0xa8, 0x80, 0x20, 0xf2, 0xd5, 0xd6, 0xcc, 0x2c }, /* mdata */
      24, /* mdata_len */
      16, /* mic_len */
      { 0x27, 0xed, 0x90, 0x66, 0x81, 0x74, 0xeb, 0xf8,
        0x24, 0x1a, 0x3c, 0x74, 0xb3, 0x5e, 0x12, 0x46,
        0xb6, 0x61, 0x7e, 0x41, 0x23, 0x57, 0x8f, 0x15,
        0x3b, 0xdb, 0x67, 0x06, 0x2a, 0x13, 0xef, 0x4e,
        0x98, 0x6f, 0x5b, 0xb3, 0xd0, 0xbb, 0x43, 0x07 } /* expected */
    }, {
      true, /* encrypt */
      2, /* len_len */
      2, /* key_size_index */
      6, /* key_area */
      { 0x2e, 0x1e, 0x01, 0x32, 0x46, 0x85, 0x00, 0xd4,
        0xbd, 0x47, 0x86, 0x25, 0x63 }, /* nonce */
      { 0x3c, 0x5f, 0x54, 0x04, 0x37, 0x0a, 0xbd, 0xcb,
        0x1e, 0xdd, 0xe9, 0x9d, 0xe6, 0x0d, 0x06, 0x82,
        0xc6, 0x00, 0xb0, 0x34, 0xe0, 0x63, 0xb7, 0xd3,
        0x23, 0x77, 0x23, 0xda, 0x70, 0xab, 0x75, 0x52 }, /* adata */
      32, /* adata_len */
      { 0x23, 0x90, 0x29, 0xf1, 0x50, 0xbc, 0xcb, 0xd6,
        0x7e, 0xdb, 0xb6, 0x7f, 0x8a, 0xe4, 0x56, 0xb4,
        0xea, 0x06, 0x6a, 0x4b, 0xee, 0xe0, 0x65, 0xf9 }, /* mdata */
      24, /* mdata_len */
      4, /* mic_len */
      { 0x9c, 0x8d, 0x5d, 0xd2, 0x27, 0xfd, 0x9f, 0x81,
        0x23, 0x76, 0x01, 0x83, 0x0a, 0xfe, 0xe4, 0xf0,
        0x11, 0x56, 0x36, 0xc8, 0xe5, 0xd5, 0xfd, 0x74,
        0x3c, 0xb9, 0xaf, 0xed } /* expected */
    }, {
      false, /* decrypt */
      2, /* len_len */
      2, /* key_size_index */
      0, /* key_area */
      { 0x72, 0xa6, 0x0f, 0x34, 0x5a, 0x19, 0x78, 0xfb,
        0x40, 0xf2, 0x8a, 0x2f, 0xa4 }, /* nonce */
      {}, /* adata */
      0, /* adata_len */
      { 0x55, 0xf0, 0x68, 0xc0, 0xbb, 0xba, 0x8b, 0x59,
        0x80, 0x13, 0xdd, 0x18, 0x41, 0xfd, 0x74, 0x0f,
        0xda, 0x29, 0x02, 0x32, 0x21, 0x48, 0xab, 0x5e,
        0x93, 0x57, 0x53, 0xe6, 0x01, 0xb7, 0x9d, 0xb4,
        0xae, 0x73, 0x0b, 0x6a, 0xe3, 0x50, 0x07, 0x31 }, /* mdata */
      40, /* mdata_len */
      16, /* mic_len */
      { 0x30, 0xd5, 0x6f, 0xf2, 0xa2, 0x5b, 0x83, 0xfe,
        0xe7, 0x91, 0x11, 0x0f, 0xca, 0xea, 0x48, 0xe4,
        0x1d, 0xb7, 0xc7, 0xf0, 0x98, 0xa8, 0x10, 0x00 } /* expected */
    }, {
      false, /* decrypt */
      2, /* len_len */
      2, /* key_size_index */
      2, /* key_area */
      { 0x29, 0x1e, 0x91, 0xb1, 0x9d, 0xe5, 0x18, 0xcd,
        0x78, 0x06, 0xde, 0x44, 0xf6 }, /* nonce */
      { 0xb4, 0xf8, 0x32, 0x69, 0x44, 0xa4, 0x5d, 0x95,
        0xf9, 0x18, 0x87, 0xc2, 0xa6, 0xac, 0x36, 0xb6,
        0x0e, 0xea, 0x5e, 0xde, 0xf8, 0x4c, 0x1c, 0x35,
        0x81, 0x46, 0xa6, 0x66, 0xb6, 0x87, 0x83, 0x35 }, /* adata */
      32, /* adata_len */
      { 0xca, 0x48, 0x2c, 0x67, 0x4b, 0x59, 0x90, 0x46,
        0xcc, 0x7d, 0x7e, 0xe0, 0xd0, 0x0e, 0xec, 0x1e }, /* mdata */
      16, /* mdata_len */
      16, /* mic_len */
      {} /* expected */
    }, {
      false, /* decrypt */
      8, /* len_len */
      2, /* key_size_index */
      4, /* key_area */
      { 0xaa, 0xa2, 0x3f, 0x10, 0x16, 0x47, 0xd8 }, /* nonce */
      { 0xa3, 0x55, 0xd4, 0xc6, 0x11, 0x81, 0x2e, 0x5f,
        0x92, 0x58, 0xd7, 0x18, 0x8b, 0x3d, 0xf8, 0x85,
        0x14, 0x77, 0x09, 0x4f, 0xfc, 0x2a, 0xf2, 0xcf,
        0x0c, 0x86, 0x70, 0xdb, 0x90, 0x3f, 0xbb, 0xe0 }, /* adata */
      32, /* adata_len */
      { 0x27, 0xed, 0x90, 0x66, 0x81, 0x74, 0xeb, 0xf8,
        0x24, 0x1a, 0x3c, 0x74, 0xb3, 0x5e, 0x12, 0x46,
        0xb6, 0x61, 0x7e, 0x41, 0x23, 0x57, 0x8f, 0x15,
        0x3b, 0xdb, 0x67, 0x06, 0x2a, 0x13, 0xef, 0x4e,
        0x98, 0x6f, 0x5b, 0xb3, 0xd0, 0xbb, 0x43, 0x07 }, /* mdata */
      40, /* mdata_len */
      16, /* mic_len */
      { 0x64, 0x4e, 0xb3, 0x4b, 0x9a, 0x12, 0x6e, 0x43,
        0x7b, 0x5e, 0x01, 0x5e, 0xea, 0x14, 0x1c, 0xa1,
        0xa8, 0x80, 0x20, 0xf2, 0xd5, 0xd6, 0xcc, 0x2c } /* expected */
    }, {
      false, /* decrypt */
      2, /* len_len */
      2, /* key_size_index */
      6, /* key_area */
      { 0x2e, 0x1e, 0x01, 0x32, 0x46, 0x85, 0x00, 0xd4,
        0xbd, 0x47, 0x86, 0x25, 0x63 }, /* nonce */
      { 0x3c, 0x5f, 0x54, 0x04, 0x37, 0x0a, 0xbd, 0xcb,
        0x1e, 0xdd, 0xe9, 0x9d, 0xe6, 0x0d, 0x06, 0x82,
        0xc6, 0x00, 0xb0, 0x34, 0xe0, 0x63, 0xb7, 0xd3,
        0x23, 0x77, 0x23, 0xda, 0x70, 0xab, 0x75, 0x52 }, /* adata */
      32, /* adata_len */
      { 0x9c, 0x8d, 0x5d, 0xd2, 0x27, 0xfd, 0x9f, 0x81,
        0x23, 0x76, 0x01, 0x83, 0x0a, 0xfe, 0xe4, 0xf0,
        0x11, 0x56, 0x36, 0xc8, 0xe5, 0xd5, 0xfd, 0x74,
        0x3c, 0xb9, 0xaf, 0xed }, /* mdata */
      28, /* mdata_len */
      4, /* mic_len */
      { 0x23, 0x90, 0x29, 0xf1, 0x50, 0xbc, 0xcb, 0xd6,
        0x7e, 0xdb, 0xb6, 0x7f, 0x8a, 0xe4, 0x56, 0xb4,
        0xea, 0x06, 0x6a, 0x4b, 0xee, 0xe0, 0x65, 0xf9 } /* expected */
    }
  };
  static uint8_t adata[ADATA_MAX_LEN];
  static uint8_t mdata[MDATA_MAX_LEN];
  static uint8_t mic[MIC_MAX_LEN];
  static int i;
  static uint8_t key_size_index = -1, ret;
  static rtimer_clock_t time, time2, total_time;

  PROCESS_BEGIN();

  puts("-----------------------------------------\n"
       "Initializing cryptoprocessor...");
  crypto_init();

  for(i = 0; i < sizeof(vectors) / sizeof(vectors[0]); i++) {
    if(key_size_index != vectors[i].key_size_index) {
      key_size_index = vectors[i].key_size_index;
      printf("-----------------------------------------\n"
             "Filling %d-bit key store...\n", 128 + (key_size_index << 6));
      time = RTIMER_NOW();
      ret = aes_load_keys(keys[key_size_index].keys,
              keys[key_size_index].key_size, keys[key_size_index].count, 0);
      time = RTIMER_NOW() - time;
      printf("aes_load_keys(): %s, %lu us\n", str_res[ret],
             (uint32_t)((uint64_t)time * 1000000 / RTIMER_SECOND));
      PROCESS_PAUSE();
      if(ret != CRYPTO_SUCCESS) {
        break;
      }
    }

    printf("-----------------------------------------\n"
           "Test vector #%d: %s\n"
           "len_len=%d key_area=%d\n"
           "adata_len=%d mdata_len=%d mic_len=%d\n",
           i, vectors[i].encrypt ? "encrypt" : "decrypt",
           vectors[i].len_len, vectors[i].key_area,
           vectors[i].adata_len, vectors[i].mdata_len, vectors[i].mic_len);

    /* adata and mdata have to be in SRAM. */
    rom_util_memcpy(adata, vectors[i].adata, vectors[i].adata_len);
    rom_util_memcpy(mdata, vectors[i].mdata, vectors[i].mdata_len);

    time = RTIMER_NOW();
    if(vectors[i].encrypt) {
      ret = ccm_auth_encrypt_start(vectors[i].len_len, vectors[i].key_area,
                                   vectors[i].nonce, adata,
                                   vectors[i].adata_len, mdata,
                                   vectors[i].mdata_len, mdata,
                                   vectors[i].mic_len, &ccm_test_process);
      time2 = RTIMER_NOW();
      time = time2 - time;
      total_time = time;
      if(ret == CRYPTO_SUCCESS) {
        PROCESS_WAIT_EVENT_UNTIL(ccm_auth_encrypt_check_status());
        time2 = RTIMER_NOW() - time2;
        total_time += time2;
      }
      printf("ccm_auth_encrypt_start(): %s, %lu us\n", str_res[ret],
             (uint32_t)((uint64_t)time * 1000000 / RTIMER_SECOND));
      if(ret != CRYPTO_SUCCESS) {
        PROCESS_PAUSE();
        continue;
      }
      printf("ccm_auth_encrypt_check_status() wait: %lu us\n",
             (uint32_t)((uint64_t)time2 * 1000000 / RTIMER_SECOND));

      time = RTIMER_NOW();
      ret = ccm_auth_encrypt_get_result(mic, vectors[i].mic_len);
      time = RTIMER_NOW() - time;
      total_time += time;
      printf("ccm_auth_encrypt_get_result(): %s, %lu us\n", str_res[ret],
             (uint32_t)((uint64_t)time * 1000000 / RTIMER_SECOND));
      PROCESS_PAUSE();
      if(ret != CRYPTO_SUCCESS) {
        continue;
      }

      if(rom_util_memcmp(mdata, vectors[i].expected, vectors[i].mdata_len)) {
        puts("Encrypted message does not match expected one");
      } else {
        puts("Encrypted message OK");
      }

      if(rom_util_memcmp(mic, vectors[i].expected + vectors[i].mdata_len,
                         vectors[i].mic_len)) {
        puts("MIC does not match expected one");
      } else {
        puts("MIC OK");
      }
    } else {
      ret = ccm_auth_decrypt_start(vectors[i].len_len, vectors[i].key_area,
                                   vectors[i].nonce, adata,
                                   vectors[i].adata_len, mdata,
                                   vectors[i].mdata_len, mdata,
                                   vectors[i].mic_len, &ccm_test_process);
      time2 = RTIMER_NOW();
      time = time2 - time;
      total_time = time;
      if(ret == CRYPTO_SUCCESS) {
        PROCESS_WAIT_EVENT_UNTIL(ccm_auth_decrypt_check_status());
        time2 = RTIMER_NOW() - time2;
        total_time += time2;
      }
      printf("ccm_auth_decrypt_start(): %s, %lu us\n", str_res[ret],
             (uint32_t)((uint64_t)time * 1000000 / RTIMER_SECOND));
      if(ret != CRYPTO_SUCCESS) {
        PROCESS_PAUSE();
        continue;
      }
      printf("ccm_auth_decrypt_check_status() wait: %lu us\n",
             (uint32_t)((uint64_t)time2 * 1000000 / RTIMER_SECOND));

      time = RTIMER_NOW();
      ret = ccm_auth_decrypt_get_result(mdata, vectors[i].mdata_len,
                                        mic, vectors[i].mic_len);
      time = RTIMER_NOW() - time;
      total_time += time;
      printf("ccm_auth_decrypt_get_result(): %s, %lu us\n", str_res[ret],
             (uint32_t)((uint64_t)time * 1000000 / RTIMER_SECOND));
      PROCESS_PAUSE();
      if(ret != CRYPTO_SUCCESS) {
        continue;
      }

      if(rom_util_memcmp(mdata, vectors[i].expected,
                         vectors[i].mdata_len - vectors[i].mic_len)) {
        puts("Decrypted message does not match expected one");
      } else {
        puts("Decrypted message OK");
      }
    }

    printf("Total duration: %lu us\n",
           (uint32_t)((uint64_t)total_time * 1000000 / RTIMER_SECOND));
  }

  puts("-----------------------------------------\n"
       "Disabling cryptoprocessor...");
  crypto_disable();

  puts("Done!");

  PROCESS_END();
}
/*---------------------------------------------------------------------------*/
/**
 * @}
 * @}
 */
